package com.woai.vichat.mine.act;
import com.woai.vichat.R;
import android.os.Bundle;
import android.view.View;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.woai.vichat.base.act.BasePActivity;
import com.woai.vichat.mine.adapter.MyDynamicListAdapter;
import com.woai.vichat.mine.entity.MyDynamicListModel;
import com.woai.vichat.mine.presenter.MyDynamicListPresenter;
import com.woai.common.util.IntentUtils;
import com.woai.common.util.ToastUtil;

/**
 * MyDynamicListAct
 * Created on 2018-06-22 17:08:20.
 * @author WuGefei
 */
public class MyDynamicListAct extends BasePActivity<MyDynamicListAct, MyDynamicListPresenter> implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener{

         <activity
           android:name="com.woai.vichat.mine.act.MyDynamicListAct"
           android:screenOrientation="portrait"/>

    @BindView(R.id.titleBar)
    RelativeLayout titleBar;

    @BindView(R.id.tvTopTitle)
    TextView tvTitle;

    @BindView(R.id.iv_back)
    ImageView ivBack;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    private int currentPos = 1;

    private MyDynamicListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_dynamic_list);
        init();
    }

    protected void init() {
        ButterKnife.bind(this);
        titleBar.setVisibility(View.VISIBLE);
        tvTitle.setText("MyDynamicList");
        initAdapter();
        request();
    }

    private void request(){
        presenter.request(currentPos, this);
    }

    private void initAdapter(){
        mAdapter = new MyDynamicListAdapter(R.layout.item_my_dynamic_list, null);
        mAdapter.setEmptyView(R.layout.empty_view, (ViewGroup) recyclerView.getParent());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(this, recyclerView);
        swipeRefresh.setOnRefreshListener(this);
    }

    public void bindData(MyDynamicListModel model){
        mAdapter.loadMoreComplete();
        swipeRefresh.setRefreshing(false);
        if (model.getCurrentPage() == 1) {
            mAdapter.setNewData(model.getData());
        } else {
            if (model.getData().size() > 0) {
                mAdapter.addData(model.getData());
            }
        }

        if(model==null){

        }

        //if (currentPos >= data.getPageTotal()) {
            //第一页如果不够一页就不显示没有更多数据布局
        //    mAdapter.loadMoreEnd(false);
        //}
    }

	@Override
    public void onRefresh() {
        currentPos = 1;
        request();
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void onLoadMoreRequested() {
        currentPos++;
        request();
    }

    @Override
    protected MyDynamicListPresenter createPresenter() {
        return new MyDynamicListPresenter();
    }
}